//
//  WBVisitorView.swift
//  WinsonWeibo
//
//  Created by Administrator on 2017/4/17.
//  Copyright © 2017年 Winson. All rights reserved.
//

import UIKit

/// 访客视图
class WBVisitorView: UIView {

    //MARK: 访客视图字典属性
    var visitorinfo: [String:String]? {
        didSet {
            // 取字典信息
            guard let imageName = visitorinfo?["imageName"],
                let message = visitorinfo?["message"] else {

                    return
            }
            // 2. 设置消息
            tipLabel.text = message

            // 3. 首页不需要设置 设置 imageName = ""
            if imageName == "" {
                startAnimation()
                return
            }
            aniImageView.image = UIImage(named: imageName)

            // 4. 其它控制器的访客视图不需要显示小房子 和遮罩视图
            inconImageView.isHidden = true
            maskIconImageView.isHidden = true

        }
    }


    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    /// 旋转图标动画
    private func startAnimation() {

        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * Double.pi
        anim.repeatCount = MAXFLOAT
        anim.duration = 15

        // 动画完成后不删除，如果aniImageView 被释放，动画将一起被释放
        // 在设置连续播放动画时非常有用
        anim.isRemovedOnCompletion = false

        // 将动画添加到图层
        aniImageView.layer.add(anim, forKey: nil)

    }

    // 添加访客视图控件
    // 背景转动的小圆圈
    lazy var aniImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    // 遮罩层
    lazy var maskIconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    // 小房子
    lazy var inconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    // 提示标签控件
    lazy var tipLabel: UILabel = UILabel.cz_label(
        withText: "关注一些人，回这里看看有什么惊喜。关注一些人，回这里看看有什么惊喜",
        fontSize: 14,
        color: UIColor.darkGray)
    // 注册按钮
    lazy var registerButton: UIButton = UIButton.cz_textButton(
        "注册",
        fontSize: 16,
        normalColor: UIColor.orange,
        highlightedColor: UIColor.black,
        backgroundImageName: "common_button_white_disable")
    // 登录按钮
    lazy var loginButton: UIButton = UIButton.cz_textButton(
        "登录",
        fontSize: 16,
        normalColor: UIColor.darkGray,
        highlightedColor: UIColor.black,
        backgroundImageName: "common_button_white_disable")

}

// MARK: - 设置界面
extension WBVisitorView {

    func setupUI() {

        backgroundColor = UIColor.cz_color(withRed: 237, green: 237, blue: 237)

        // 添加控件
        addSubview(aniImageView)
        addSubview(maskIconImageView)
        addSubview(inconImageView)
        addSubview(tipLabel)
        addSubview(registerButton)
        addSubview(loginButton)

        // 添加自动布局
        // 1. 禁用 Autoresizing
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        // 文本居中
        tipLabel.textAlignment = NSTextAlignment.center

        // 2. 背景转动图像
        addConstraint(NSLayoutConstraint(
            item: aniImageView,
            attribute: NSLayoutAttribute.centerX,
            relatedBy: NSLayoutRelation.equal,
            toItem: self,
            attribute: NSLayoutAttribute.centerX,
            multiplier: 1.0,
            constant: 0))
        addConstraint(NSLayoutConstraint(
            item: aniImageView,
            attribute: NSLayoutAttribute.centerY,
            relatedBy: NSLayoutRelation.equal,
            toItem: self,
            attribute: NSLayoutAttribute.centerY,
            multiplier: 1.0,
            constant: -60))
        // 3. 添加小房子
        addConstraint(NSLayoutConstraint(
            item: inconImageView,
            attribute: NSLayoutAttribute.centerX,
            relatedBy: NSLayoutRelation.equal,
            toItem: aniImageView,
            attribute: NSLayoutAttribute.centerX,
            multiplier: 1.0,
            constant: 0))
        addConstraint(NSLayoutConstraint(
            item: inconImageView,
            attribute: NSLayoutAttribute.centerY,
            relatedBy: NSLayoutRelation.equal,
            toItem: aniImageView,
            attribute: NSLayoutAttribute.centerY,
            multiplier: 1.0,
            constant: 0))
        // 4. 提示标签
        addConstraint(NSLayoutConstraint(
            item: tipLabel,
            attribute: NSLayoutAttribute.centerX,
            relatedBy: NSLayoutRelation.equal,
            toItem: aniImageView,
            attribute: NSLayoutAttribute.centerX,
            multiplier: 1.0,
            constant: 0))
        addConstraint(NSLayoutConstraint(
            item: tipLabel,
            attribute: NSLayoutAttribute.top,
            relatedBy: NSLayoutRelation.equal,
            toItem: aniImageView,
            attribute: NSLayoutAttribute.bottom,
            multiplier: 1.0,
            constant: 20))
        addConstraint(NSLayoutConstraint(
            item: tipLabel,
            attribute: NSLayoutAttribute.width,
            relatedBy: NSLayoutRelation.equal,
            toItem: nil,
            attribute: NSLayoutAttribute.notAnAttribute,
            multiplier: 1.0,
            constant: 250))
        // 添加注册按钮
        addConstraint(NSLayoutConstraint(
            item: registerButton,
            attribute: NSLayoutAttribute.left,
            relatedBy: NSLayoutRelation.equal,
            toItem: tipLabel,
            attribute: NSLayoutAttribute.left,
            multiplier: 1.0,
            constant: 0))
        addConstraint(NSLayoutConstraint(
            item: registerButton,
            attribute: NSLayoutAttribute.top,
            relatedBy: NSLayoutRelation.equal,
            toItem: tipLabel,
            attribute: NSLayoutAttribute.bottom,
            multiplier: 1.0,
            constant: 20))
        addConstraint(NSLayoutConstraint(
            item: registerButton,
            attribute: NSLayoutAttribute.width,
            relatedBy: NSLayoutRelation.equal,
            toItem: nil,
            attribute: NSLayoutAttribute.notAnAttribute,
            multiplier: 1.0,
            constant: 100))
        // 添加登录按钮
        addConstraint(NSLayoutConstraint(
            item: loginButton,
            attribute: NSLayoutAttribute.right,
            relatedBy: NSLayoutRelation.equal,
            toItem: tipLabel,
            attribute: NSLayoutAttribute.right,
            multiplier: 1.0,
            constant: 0))
        addConstraint(NSLayoutConstraint(
            item: loginButton,
            attribute: NSLayoutAttribute.top,
            relatedBy: NSLayoutRelation.equal,
            toItem: tipLabel,
            attribute: NSLayoutAttribute.bottom,
            multiplier: 1.0,
            constant: 20))
        addConstraint(NSLayoutConstraint(
            item: loginButton,
            attribute: NSLayoutAttribute.width,
            relatedBy: NSLayoutRelation.equal,
            toItem: nil,
            attribute: NSLayoutAttribute.notAnAttribute,
            multiplier: 1.0,
            constant: 100))
        // 设置遮罩图像
        let metrics = ["spacing":10]

        addConstraints(NSLayoutConstraint.constraints(
            withVisualFormat: "H:|-0-[maskIconImageView]-0-|",
            options: [],
            metrics: nil,
            views: ["maskIconImageView":maskIconImageView]))
        addConstraints(NSLayoutConstraint.constraints(
            withVisualFormat: "V:|-0-[maskIconImageView]-(spacing)-[registerButton]",
            options: [],
            metrics: metrics,
            views: ["maskIconImageView":maskIconImageView, "registerButton":registerButton]))
    }
}






























